<%--
  Created by IntelliJ IDEA.
  User: parttime
  Date: 2017/8/19
  Time: 下午16:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="resources.jsp" %>


<html>
<head>
    <title>商家列表</title>
</head>
<body>
<div>
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">查询条件</h3>
        </div>
        <div class="panel-body">
            <form class="form-inline ems_search_form">
                <div class="form-group">
                    <%--<label for="nick_name">店主：</label>--%>
                    <input type="text" class="form-control" id="nick_name" name="nick_name"  placeholder="店主">
                </div>
                <div class="form-group">
                    <%--<label for="store_name_">店铺名：</label>--%>
                    <input type="text" class="form-control" id="store_name_" name="store_name_" placeholder="店铺名">
                </div>
                <div class="form-group">
                    <%--<label for="contact_">联系方式：</label>--%>
                    <input type="text" class="form-control" id="contact_" name="contact_" placeholder="联系方式">
                </div>
                <div class="form-group">
                    <%--<label for="type_">店铺类型：</label>--%>
                    <select id="type_" name="type_" class="form-control">
                        <option value = "">请选择</option>
                        <option value ="online_store">线上店</option>
                        <option value ="oto">线上实体店</option>
                    </select>
                </div>
                <div class="form-group">
                    <button type="button" class="btn btn-info btn-search">查询</button>
                    <button type="button" class="btn btn-default btn-reset">清空</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="ems_table_content">
    <div class="panel panel-default">
        <div class="panel-heading">商家列表</div>
        <div id="table_tools">
            <div class="form-inline" role="form">
                <button type="button" id="shop_recommend" class="btn btn-outline btn-primary">
                    <i class="glyphicon glyphicon-heart" aria-hidden="true"></i>
                    推荐商家
                </button>
                <button type="button" id="shop_unrecommend" class="btn btn-outline btn-danger">
                    <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                    取消推荐商家
                </button>
            </div>
        </div>
        <table id="shopTable"
               data-url="/ems/shop/pagination"
               data-show-export="true">
            <thead>
            <tr>
                <th data-field="state" data-checkbox="true"></th>
                <th data-field="nick_name">店主</th>
                <th data-field="store_name_">店铺名</th>
                <th data-field="business_type_" data-formatter="typeFormat" >店铺类型</th>
                <th data-field="contact_">联系方式</th>
                <th data-field="addr_">地址</th>
                <th data-field="recommend" data-formatter="recommendFormat">推荐状态</th>
                <th data-field="create_time_">创建时间</th>
                <th data-field="status_" data-formatter="statusFormat">审核状态</th>
            </tr>
            </thead>

        </table>
    </div>
</div>

<script>
    $(function () {
        $("#shopTable").createTable();

        //搜索
        $("#btn-search").on("click",function () {
            search(getQueryParams());
        });

        $("#shop_recommend").on("click",function(){

            var selectRows =  $('#shopTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要推荐的商家");
                return;
            }

            var shopIdList = "";
            for (var i = 0;i<selectRows.length;i++) {
                if(selectRows[i].status_ != 'audit_succeed'){
                    $.errorAlert("推荐的商家必须已审核");
                    return;
                }

                if(selectRows[i].recommend == 'Y'){
                    $.errorAlert("不能重复推荐");
                    return;
                }
                shopIdList += selectRows[i].id_ + ",";

            }
            shopIdList = shopIdList.substring(0,shopIdList.length - 1);
            $.confirmAlert("确定要推荐这些商家吗",function(){
                $.emsAjax({
                    data:{shopIdList:shopIdList},
                    type:"POST",
                    url:"/ems/shop/recommend",
                    success:function(resp) {
                        if (resp.status == "000") {
                            $.alert(resp.message);
                            search("#shopTable",getQueryParams());
                        }else {
                            $.errorAlert(resp.message);
                        }
                    }
                });
            });
        })

        //取消推荐
        $("#shop_unrecommend").on("click",function(){

            var selectRows =  $('#shopTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要取消推荐的商家");
                return;
            }

            var shopIdList = "";
            for (var i = 0;i<selectRows.length;i++) {
                if(selectRows[i].recommend == 'N'){
                    $.errorAlert("不能重复取消推荐");
                    return;
                }
                shopIdList += selectRows[i].id_ + ",";

            }
            shopIdList = shopIdList.substring(0,shopIdList.length - 1);
            $.confirmAlert("确定要取消推荐这些商家吗",function(){
                $.emsAjax({
                    data:{shopIdList:shopIdList},
                    type:"POST",
                    url:"/ems/shop/unrecommend",
                    success:function(resp) {
                        if (resp.status == "000") {
                            $.alert(resp.message);
                            search("#shopTable",getQueryParams());
                        }else {
                            $.errorAlert(resp.message);
                        }
                    }
                });
            });
        })

    })

</script>


<script>
    function statusFormat(srcValue) {
        if (srcValue == 'auditing') {
            return "<span style='color: red;font-weight: bold' >审核中</span>";
        } else if(srcValue == 'audit_fail') {
            return "<span style='color: #3cb371;font-weight: bold'>审核失败</span>";
        } else if (srcValue == 'audit_succeed') {
            return "<span style='color: #3cb371;font-weight: bold'>审核成功</span>";
        }
    }

    function recommendFormat(srcValue) {
        if (srcValue == "N") {
            return "<span style='color: red;font-weight: bold' >未推荐</span>";
        } else {
            return "<span style='color: #3cb371;font-weight: bold'>已推荐</span>";
        }
    }

    function typeFormat(srcValue) {
        if (srcValue == 'online_store') {
            return "<span style='color: #0dce4e;font-weight: bold' >线上实体店</span>";
        }else if(srcValue == 'oto') {
            return "<span style='color: #1945b3;font-weight: bold'>线下实体店</span>";
        }
    }

    function search(params) {
        var queryParams = {"query":params};
        $('#shopTable').bootstrapTable('refresh', queryParams);
    }

</script>

</body>
</html>
